$(function () {
    var emp_datagrid = $("#emp_datagrid");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");

    var emp_datagrid2 = $("#emp_datagrid2");
    var emp_dialog2 = $("#emp_dialog2");
    var emp_form2 = $("#emp_form2");

    emp_datagrid.datagrid({
        url:'/dictionary/query.do',
        fit:true,
        fitColumns:true,
        columns:[
            [
                {field:'sn',title:'字典目录编码',width:100},
                {field:'name',title:'字典目录名称',width:100},
                {field:'remark',title:'字典目录简介',width:100},
            ]
        ],
        rownumbers:true,
        singleSelect:true,
        toolbar:'#tb',
        onClickRow:function (index, row) {

            emp_datagrid2.datagrid({
                url:'/dictionaryBill/selectBySn.do?sn='+row.sn,
                fit:true,
                fitColumns:true,
                columns:[
                    [
                        {field:'sn',title:'字典目录编号',width:100},
                        {field:'name',title:'字典明细名称',width:100},
                        {field:'remark',title:'字典明细简介',width:100},
                    ]
                ],
                rownumbers:true,
                singleSelect:true,
                toolbar:'#tb2'
            })

        }
    })

   

    emp_dialog.dialog({
        width:320,
        height:320,
        buttons:'#form_btn',
        closed:true,
        onClose:function () {
            emp_form.form('clear')
        }
    })

    var methodObj = {
        add:function() {
            emp_dialog.dialog('open');
            emp_dialog.dialog('setTitle','新增目录');
            $("#password_tr").show();
        },
        edit:function() {
            var row = emp_datagrid.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }



            emp_form.form('load',row);

            emp_dialog.dialog('open');
            emp_dialog.dialog('setTitle','编辑目录');
        },
        del:function() {
            var row = emp_datagrid.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }
            $.messager.confirm("确认对话框","你确定要执行操作吗?",function (r) {
                if(r){
                    $.get('/dictionary/delete.do',{id:row.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示","删除成功",'info',function () {
                                methodObj['cancel']();
                                methodObj['reload']();
                            })
                        }else{
                            $.messager.alert("温馨提示",data.msg,'warning')
                        }
                    })
                }
            })
        },
        reload:function() {
            emp_datagrid.datagrid('reload');
        },
        //保存
        save:function() {
            emp_form.form("submit",{
                url:'/dictionary/saveOrUpdate.do',

                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提示","保存成功",'info',function () {
                            methodObj['cancel']();
                            methodObj['reload']();
                        })
                    }else{
                        $.messager.alert("温馨提示",data.msg,'warning')
                    }
                }
            })
        },
        cancel:function () {
            emp_dialog.dialog('close')
        }
    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })



    /**/

    emp_dialog2.dialog({
        width:320,
        height:320,
        buttons:'#form_btn2',
        closed:true,
        onClose:function () {
            emp_form2.form('clear')
        }

    })

    var methodObj2 = {
        add2:function() {
            emp_dialog2.dialog('open');
            emp_dialog2.dialog('setTitle','新增字典明细');
          var tr=  emp_datagrid.datagrid("getSelected");

            $("#inpit_sn").combobox("setValues",[tr.sn])
        },
        edit2:function() {
            var row = emp_datagrid2.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }


            emp_form2.form('load',row);
            emp_dialog2.dialog('open');
            emp_dialog2.dialog('setTitle','编辑字典明细');
        },
        del2:function() {
            var row = emp_datagrid2.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }
            $.messager.confirm("确认对话框","你确定要执行操作吗?",function (r) {
                if(r){
                    $.get('/dictionaryBill/delete.do',{id:row.id},function (data) {
                        if(data.success){
                            $.messager.alert("温馨提示",'删除成功','info',function () {
                                methodObj2['cancel2']();
                                methodObj2['reload2']();
                            })
                        }else{
                            $.messager.alert("温馨提示",data.msg,'warning')
                        }
                    })
                }
            })
        },
        reload2:function() {
            emp_datagrid2.datagrid('reload');
        },
        save2:function() {
            emp_form2.form("submit",{
                url:'/dictionaryBill/saveOrUpdate.do',
                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提示",'保存成功','info',function () {
                            methodObj2['cancel2']();
                            methodObj2['reload2']();
                        })
                    }else{
                        $.messager.alert("温馨提示",data.msg,'warning')
                    }
                }
            })
        },
        cancel2:function () {
            emp_dialog2.dialog('close')
        }
    }

    $("a[data-cmd2]").click(function () {
        var methodName = $(this).data("cmd2");
        methodObj2[methodName]();
    })
})


